/*
 * DelimitedMatrixWriter.cs
 *
 * Copyright (c) 2005, dnAnalytics. All rights reserved.
 */

#region Using Directives
using System;
using System.IO;
using dnAnalytics.Resources;
#endregion Using Directives

namespace dnAnalytics.LinearAlgebra.IO
{
    /// <summary>
    /// Writes an <see cref="Matrix"/> to delimited text file. If the user does not
    /// specify a delimiter, a comma is used.
    /// </summary>
    /// <include file='../../../../examples/trunk/LinearAlgebra/IO/DelimitedMatrixWriterOverall.xml' path='example'/> 
    public class DelimitedMatrixWriter : IMatrixWriter
    {

        #region Fields
        private string delimiter;
        #endregion Fields

        #region Constructors
        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using a comma as the delimiter.
        /// </summary>
        public DelimitedMatrixWriter()
        {
            delimiter = ",";
        }

        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using the given delimiter.
        /// </summary>
        /// <param name="delimiter">the delimiter to use.</param>
        public DelimitedMatrixWriter(char delimiter)
        {
            this.delimiter = new string(delimiter, 1);
        }

        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using the given delimiter.
        /// </summary>
        /// <param name="delimiter">the delimiter to use.</param>
        public DelimitedMatrixWriter(string delimiter)
        {
            this.delimiter = delimiter;
        }
        #endregion Constructors

        #region IMatrixWriter Members

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given file. If the file already exists, 
        /// the file will be overwritten.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="file">The file to write the matrix to.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="file"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, string file)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix", Strings.NullParameterException);
            }
            if (file == null)
            {
                throw new ArgumentNullException("file", Strings.NullParameterException);
            }
            WriteMatrix(matrix, file, null);
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given file. If the file already exists, 
        /// the file will be overwritten.
        /// </summary>
        /// <param name="matrix">the matrix to write.</param>
        /// <param name="file">The file to write the matrix to.</param>
        /// <param name="format">The format to use on each element.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="file"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, string file, string format)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix", Strings.NullParameterException);
            }
            if (file == null)
            {
                throw new ArgumentNullException("file", Strings.NullParameterException);
            }

            if (File.Exists(file))
            {
                File.Delete(file);
            }
            WriteMatrix(matrix, File.OpenWrite(file), format);
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given stream.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="stream">The <see cref="Stream"/> to write the matrix to.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="stream"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, Stream stream)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix", Strings.NullParameterException);
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream", Strings.NullParameterException);
            }
            WriteMatrix(matrix, stream, null);
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given stream.
        /// </summary>
        /// <param name="matrix">The <see cref="TextWriter"/> to write.</param>
        /// <param name="stream">The <see cref="Stream"/> to write the matrix to.</param>
        /// <param name="format">The format to use on each element.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="stream"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, Stream stream, string format)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix", Strings.NullParameterException);
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream", Strings.NullParameterException);
            }

            using (StreamWriter writer = new StreamWriter(new BufferedStream(stream)))
            {
                WriteMatrix(matrix, writer, format);
            }
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given <b>TextWriter</b>.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, TextWriter writer)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix", Strings.NullParameterException);
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer", Strings.NullParameterException);
            }
            WriteMatrix(matrix, writer, null);
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given <b>TextWriter</b>.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
        /// <param name="format">The format to use on each element.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is null.</exception>
        public void WriteMatrix(Matrix matrix, TextWriter writer, string format)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix", Strings.NullParameterException);
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer", Strings.NullParameterException);
            }

            int cols = matrix.Columns - 1;
            int rows = matrix.Rows - 1;
            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Columns; j++)
                {
                    writer.Write(matrix[i, j].ToString(format, System.Globalization.CultureInfo.InvariantCulture));
                    if (j != cols)
                    {
                        writer.Write(delimiter);
                    }
                }
                if (i != rows)
                {
                    writer.Write(Environment.NewLine);
                }
            }
        }

        #endregion
    }
}
